#include <stdio.h>
#include <memory.h>

int s[ 1000 ];
void unionset( int root1, int root2 )
{
    if ( s[ root2 ] < s[ root1 ] )
        s[ root1 ] = root2;
    else
    {
        if ( s[ root1 ] == s[ root2 ] )
            s[ root1 ]--;
        s[ root2 ] = root1;
    }
}

int find( int x )
{
    if ( s[ x ] < 0 )
        return x;
    else
        return s[ x ] = find( s[ x ] );
}

int main( )
{
    int n, m, i, a, b, t, cnt;
    bool use[ 1000 ];
    while ( scanf("%d", &n) != EOF && n )
    {
        scanf("%d", &m);
        memset( use, 1, sizeof( use ) );
        for ( i = 1; i <= n; i++ )
            s[ i ] = -1;
        for ( i = 0; i < m; i++ )
        {
            scanf("%d%d", &a, &b);
            a = find( a );
            b = find( b );
            if ( a != b ) unionset( a, b );
        }
        cnt = t = 0;
        for ( i = 1; i <= n; i++ )
        {
            t = find( i );
            //printf("%d\n", t);
            if ( use[ t ] )
            {
                cnt++;
                use[ t ] = false;
            }
        }
        printf("%d\n", cnt - 1);
    }
    return 0;
}
